<?php

class MobileIndexController extends BaseController {

    static $view_path = 'mobile_end';
    private $orderService = null;
    private $memberService = null;

    /**
     * 手机登录
     * @return mixed
     */
    public function doLogin()
    {
        $input = Input::all();
        //登录操作
        if(is_array($input) && count($input)>0){
            $repo = App::make('UserRepository');

            if(!isset($_GET['password']) || empty($_GET['password'])){
                $input['password'] = $this->getMemberService()->getAuth($input['password']);
            }

            if ($repo->login($input)) {
                //判断用户类型
                if(Auth::User()->isStaff())
                    return Redirect::action('BackDashboardController@getIndex');
                if(Auth::User()->isMember()){
                    return Redirect::action('MobileDashboardController@getIndex', $input);
                }
            } else {
                if ($repo->isThrottled($input)) {
                    $err_msg = Lang::get('confide::confide.alerts.too_many_attempts');
                } elseif ($repo->existsButNotConfirmed($input)) {
                    $err_msg = Lang::get('confide::confide.alerts.not_confirmed');
                } else {
                    $err_msg = Lang::get('confide::confide.alerts.wrong_credentials');
                }

                return Redirect::action('MobileIndexController@doLogin')
                    ->withInput(Input::except('password'))
                    ->with('error', $err_msg);
            }
        }

        return View::make(self::$view_path.'.login');
    }

    public function doWeixinLogin(){

        $memberService = new MobileMemberService();
        $repo = App::make('UserRepository');
        $input = $memberService->getDefaultUserInfo();

        if ($repo->login($input)) {
            return Redirect::action('MobileDashboardController@getIndex');
        } else {
            if(!$memberService->createMember($input)){
                echo '新用户创建失败'.var_dump($memberService->errors()->getMessages());
                return;
            }
            if ($repo->login($input)) {
                return Redirect::action('MobileDashboardController@getIndex');
            }
        }
        echo '用户认证失败';
    }

    public function doRegister(){

        $input = Input::all();
        //登录操作
        if(is_array($input) && count($input)>0){

            $input['password'] = $this->getMemberService()->getAuth($input['password']);
            $input['password_confirmation'] = $this->getMemberService()->getAuth($input['password']);

            $memberService = new MobileMemberService();
            $repo = App::make('UserRepository');
            if(!$memberService->createMember($input)){
                return Redirect::action('MobileIndexController@doRegister')
                    ->withInput(Input::except('password'))
                    ->with('error', $memberService->errors()->first());
            }
            if ($repo->login($input)) {
                if(isset($input['password_confirmation']) || empty($input['password_confirmation'])){
                    unset($input['password_confirmation']);
                }
                return Redirect::action('MobileDashboardController@getIndex', $input);
            }

            return Redirect::action('MobileIndexController@doRegister')
                ->withInput(Input::except('password'))
                ->with('error', '新用户创建失败');
        }

        return View::make(self::$view_path.'.register');
    }

    /**
     * 后台登出操作
     */
    public function doLogout(){

        Confide::logout();

        return Redirect::action('BackIndexController@doLogin')
            ->with('success', '注销成功!');
    }

    public function getOrderService() {
        if(is_null($this->orderService)){
            $this->orderService = new MobileOrderService();
        }
        return $this->orderService;
    }

    public function getMemberService() {
        if(is_null($this->memberService)){
            $this->memberService = new MobileMemberService();
        }
        return $this->memberService;
    }

}
